<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">

			<el-form-item label="停车场名" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入停车场名" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>

			<el-form-item label="手机" prop="phone">
				<el-input v-model="queryParams.phone" placeholder="请输入手机" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>

			<el-form-item label="状态" prop="state">
				<el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
					<el-option v-for="dict in dict.type.zhwy_park_state" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['parking:park:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['parking:park:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['parking:park:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['parking:park:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

<!--    -->
		<el-table v-loading="loading" :data="parkList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="主键" align="center" prop="id" />
			<el-table-column label="停车场名" align="center" prop="name" />
			<el-table-column label="车位数" align="center" prop="number" />
			<el-table-column label="停车场地址" align="center" prop="address" />
			<el-table-column label="手机" align="center" prop="phone" />
			<el-table-column label="状态" align="center" prop="state">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.zhwy_park_state" :value="scope.row.state" />
				</template>
			</el-table-column>
			<el-table-column label="已用车位数" align="center" prop="useNumber" />


				<el-table-column label="计费规则" align="center" prop="ruleId">
					<template slot-scope="scope">
						<dict-tag-Down :options="ruleList" :value="scope.row.ruleId" />
					</template>
				</el-table-column>



			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['parking:park:edit']">修改</el-button>

					<el-button size="mini" type="text" @click="onItemClick(scope.row)">打开二维码
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 二维码弹窗 -->
		<el-dialog :title="title" :visible.sync="show" width="500px" append-to-body>
			<div style="justify-content: center;align-items: center;display: flex;" ref="qrcode"></div>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="downloadCode">下 载</el-button>
				<el-button @click="cancelDownload">取 消</el-button>
			</div>
		</el-dialog>

		<!-- 添加或修改停车场管理对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="停车场名" prop="name">
					<el-input v-model="form.name" placeholder="请输入停车场名" />
				</el-form-item>
				<el-form-item label="车位数" prop="number">
					<el-input v-model="form.number" placeholder="请输入车位数" />
				</el-form-item>
				<el-form-item label="停车场地址" prop="address">
					<el-input v-model="form.address" placeholder="请输入停车场地址" />
				</el-form-item>
				<el-form-item label="手机号" prop="phone">
					<el-input v-model="form.phone" placeholder="请输入手机号" />
				</el-form-item>

				<el-form-item label="状态" prop="state">
					<el-radio-group v-model="form.state">
						<el-radio v-for="dict in dict.type.zhwy_park_state" :key="dict.value"
							:label="parseInt(dict.value)">{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="已用车位数" prop="useNumber">
					<el-input v-model="form.useNumber" placeholder="请输入内容" />
				</el-form-item>
				<el-form-item label="计费规则" prop="ruleId">
					<el-select v-model="form.ruleId" placeholder="请选择计费规则">
						<el-option v-for="dict in ruleList" :key="dict.id" :label="dict.name"
							:value="parseInt(dict.id)">
						</el-option>
					</el-select>
				</el-form-item>
				<!-- <el-form-item label="收费二维码" prop="image">
					<image-upload v-model="form.image" />
				</el-form-item> -->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	//二维码插件引入
	import QRCode from 'qrcodejs2';

	import {
		listPark,
		getPark,
		delPark,
		addPark,
		updatePark
	} from "@/api/parking/park";
	import {
		listRule,
		getRule
	} from "@/api/parking/rule";

	export default {
		name: "Park",
		dicts: ['zhwy_park_state', 'sys_user_sex'],
		data() {
			return {
				show: false, // 是否打开二维码弹窗
				qrcode: null, // 二维码
				//计费规则列表
				ruleList: [],
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 停车场管理表格数据
				parkList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					name: null,
					phone: null,
					state: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					name: [{
						required: true,
						message: "停车场名不能为空",
						trigger: "blur"
					}],
					number: [{
						required: true,
						message: "车位数不能为空",
						trigger: "blur"
					}],
					address: [{
						required: true,
						message: "停车场地址不能为空",
						trigger: "blur"
					}],
					phone: [{
						required: true,
						message: "手机号不能为空",
						trigger: "blur"
					}],
					ruleId: [{
						required: true,
						message: "收费规则不能为空",
						trigger: "blur"
					}]
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询停车场管理列表 */
			getList() {
				this.loading = true;
				listPark(this.queryParams).then(response => {
					this.parkList = response.rows;
					this.total = response.total;
					this.loading = false;
				});

				//获取停车场计费规则
				listRule().then(response => {
					this.ruleList = response.rows;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					name: null,
					number: null,
					address: null,
					phone: null,
					state: null,
					useNumber: null,
					ruleId: null,
					image: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加停车场管理";
				this.form = {
					state: 0
				}
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getPark(id).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改停车场管理";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updatePark(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addPark(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除停车场管理编号为"' + ids + '"的数据项？').then(function() {
					return delPark(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			// 下载二维码图片？？？？
			// downloadImg(row) {
			// 	const url = process.env.VUE_APP_BASE_API + '/parking/park/' + row.id;
			// 	console.log(url)
			// 	//api/parking/park/profile/upload/2024/05/09/11_20240509103938A001.png
			// 	this.$download.customDownload(url);
			// },
			    //生成二维码
			    onItemClick(row){
			      this.show = true;
			      this.title = '二维码';
			      this.$refs.qrcode.innerHTML = ""; //清空二维码,避免生成多个二维码
			      this.$nextTick(() => {
			        this.qrcode = new QRCode(this.$refs.qrcode, {
			          text: row.id, // 替换为你要生成二维码的内容
			          width: 200,
			          height: 200,
			        });
			      });
			    },

			    //下载二维码
			    downloadCode(){
			      let that = this;
			      //获取二维码中格式为imag的元素
			      const nodeList = Array.prototype.slice.call(that.qrcode._el.children)
			      const img = nodeList.find((item) => item.nodeName.toUpperCase() === 'IMG')	// 选出图片类型
			      // 构建画布
			      let canvas = document.createElement('canvas');
			      canvas.width = img.width;
			      canvas.height = img.height;
			      canvas.getContext('2d').drawImage(img, 0, 0);
			      // 构造url
			      let url = canvas.toDataURL('image/png');
			      const a = document.createElement("a");
			      a.href = url;
			      a.download = `二维码.png`;
			      // 触发a链接点击事件，浏览器开始下载文件
			      a.click();
			    },

			    // 取消
			    cancelDownload(){
			      this.show = false;
			      this.$refs.qrcode.innerHTML = ""; //清空二维码,避免生成多个二维码
			    },

			/** 导出按钮操作 */
			handleExport() {
				this.download('parking/park/export', {
					...this.queryParams
				}, `park_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
